﻿' <summary>
' BeeWit IDE Entorno de desarrollo Visual para php.
' BeeWitPHP IDE es el único entorno de desarrollo para PHP 
' de tipo WYSIWYG (What You See Is What You Get). 
' ¡Ahora si!, aproveche la potencia del lenguaje PHP 
' y desarrolle más rápidamente gracias a la programación 
' intuitiva de BeeWit.
' Copyright © 2010 Dewins Murillo Garcia 
' EMAIL: dewinsmg@gmail.com
' EMAIL: dewins@beewitsoft.com
' Este Programa es Software Libre: usted puede redistribuirlo 
' y/o modificarlo bajo los términos de la Licencia Publica General 
' GNU como es publicada por la Fundacion de Software Libre; 
' en la 3ra versión de la licencia.
' Este programa es distribuido con la esperanza de que sea útil, 
' pero SIN GARANTÍA ALGUNA; sin siquiera la garantía implícita 
' de VALOR COMERCIAL o FORMADO PARA UN PROPÓSITO EN PARTICULAR. 
' Vea la Licencia Publica General GNU para mas detalles.
' Usted debe haber recibido una copia de la Licencia Publica General 
' GNU junto con este programa. Si no, vaya a http://www.beewitsoft.com
' o en http://code.google.com/p/ajax-beewit-php-ide/
' </summary>
' <remarks></remarks>

Imports System.Text.RegularExpressions

Public Class clsRegExpr
    Private ds As New DataSet("expressions")
    Private tb_expressions As DataTable

    Structure InstancedObject
        Dim VarName As String
        Dim ClassName As String
        Dim ClassCode As String
        Dim ClassComment As String
    End Structure

    Structure Vars
        Dim VarName As String
        Dim VarAmbit As String
        Dim VarComment As String
    End Structure

    Structure Methods
        Dim MethodName As String
        Dim MethodAmbit As String
        Dim MethodParameters As String
        Dim MethodComment As String
    End Structure

    Structure Events
        Dim EventName As String
    End Structure

    Public Sub New()
        ds.ReadXml(Application.StartupPath & "\resources\xml\expressions.xml")
        tb_expressions = ds.Tables("expression")
    End Sub

    Public Function ReplaceControlsCJXToIframe(ByVal Content As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "replace_cjx_to_iframe" Then
                Content = ExpReplace(Content, ExpRow("search"), ExpRow("replace"))
            End If
        Next
        Return Content
    End Function

    Public Function ReplaceAttributeClassCode(ByVal Content As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "replace_attribute_class_code" Then
                Dim strReplace As String = ExpRow("replace")
                strReplace = Replace(strReplace, "REPLACE_TEMP", TempDir)
                Content = ExpReplace(Content, ExpRow("search"), strReplace)
            End If
        Next
        Return Content
    End Function

    Public Function GetUserControlsAsIframe(ByVal Content As String) As DataTable
        If Content Is Nothing Then Return Nothing
        Dim dt As New DataTable("controls")
        Dim dcId As New DataColumn("id")
        Dim dcName As New DataColumn("name")
        Dim dcClassCode As New DataColumn("class_code")
        Dim dcIframe As New DataColumn("iframe")
        dt.Columns.Add(dcId)
        dt.Columns.Add(dcName)
        dt.Columns.Add(dcClassCode)
        dt.Columns.Add(dcIframe)
        Content = Content.Replace(vbCrLf, " ").ToLower
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "get_user_controls_as_iframe" Then
                Dim r As Regex
                Dim m As Match
                Dim mc As MatchCollection
                r = New Regex(ExpRow("search"), RegexOptions.IgnoreCase)
                mc = r.Matches(Content)
                For Each m In mc
                    Dim dr As DataRow = dt.NewRow
                    dr("name") = m.Groups("name").ToString
                    dr("id") = m.Groups("id").ToString
                    dr("class_code") = m.Groups("class_code").ToString
                    dr("iframe") = m.Groups(0).ToString
                    dt.Rows.Add(dr)
                Next
            End If
        Next
        Return dt
    End Function

    Public Function GetHTMLProperties(ByVal Content As String) As DataTable
        Dim dt As New DataTable("controls")
        Dim dcPropertyName As New DataColumn("propertyname")
        Dim dcPropertyValue As New DataColumn("propertyvalue")
        dt.Columns.Add(dcPropertyName)
        dt.Columns.Add(dcPropertyValue)
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "get_html_properties" Then
                Dim r As Regex
                Dim m As Match
                Dim mc As MatchCollection
                r = New Regex(ExpRow("search"), RegexOptions.IgnoreCase)
                mc = r.Matches(Content)
                For Each m In mc
                    Dim dr As DataRow = dt.NewRow
                    dr("propertyname") = m.Groups("propertyname").ToString
                    dr("propertyvalue") = m.Groups("propertyvalue").ToString
                    dt.Rows.Add(dr)
                Next
            End If
        Next
        Return dt
    End Function

    Public Function GetDesignAttibutes(ByVal Content As String) As DataTable
        Dim dt As New DataTable("controls")
        Dim dcDesignAttributes As New DataColumn("designattributes")
        dt.Columns.Add(dcDesignAttributes)
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "designattributes" Then
                Dim r As Regex
                Dim m As Match
                Dim mc As MatchCollection
                r = New Regex(ExpRow("search"), RegexOptions.IgnoreCase)
                mc = r.Matches(Content)
                For Each m In mc
                    Dim dr As DataRow = dt.NewRow
                    dr("designattributes") = m.Groups("designattributes").ToString
                    dt.Rows.Add(dr)
                Next
            End If
        Next
        Return dt
    End Function

    Private Function ExpReplace(ByVal Content As String, ByVal ExprSearch As String, ByVal ExprReplace As String) As String
        Dim r As Regex
        r = New Regex(ExprSearch, RegexOptions.IgnoreCase)
        If Not Content Is Nothing Then
            Return r.Replace(Content, ExprReplace)
        Else
            Return ""
        End If
    End Function

    Public Function ReplaceControlsIframeToCJX(ByVal Content As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "replace_iframe_to_cjx" Then
                Content = ExpReplace(Content, ExpRow("search"), ExpRow("replace"))
            End If
        Next
        Return Content
    End Function

    Public Function DeleteSrcInCjx(ByVal Content As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "delete_src_in_cjx" Then
                Content = ExpReplace(Content, ExpRow("search"), ExpRow("replace"))
            End If
        Next
        Return Content
    End Function

    Public Function DeleteFrameborderInCjx(ByVal Content As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "delete_frameborder_in_cjx" Then
                Content = ExpReplace(Content, ExpRow("search"), ExpRow("replace"))
            End If
        Next
        Return Content
    End Function

    Public Function DeleteScrollingInCjx(ByVal Content As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "delete_scrolling_in_cjx" Then
                Content = ExpReplace(Content, ExpRow("search"), ExpRow("replace"))
            End If
        Next
        Return Content
    End Function

    Public Function DeleteNoResizeInCjx(ByVal Content As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "delete_noresize_in_cjx" Then
                Content = ExpReplace(Content, ExpRow("search"), ExpRow("replace"))
            End If
        Next
        Return Content
    End Function

    Public Function DeleteBorderInCjx(ByVal Content As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "delete_border_in_cjx" Then
                Content = ExpReplace(Content, ExpRow("search"), ExpRow("replace"))
            End If
        Next
        Return Content
    End Function

    Public Function DeleteFrameSpacingInCjx(ByVal Content As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "delete_framespacing_in_cjx" Then
                Content = ExpReplace(Content, ExpRow("search"), ExpRow("replace"))
            End If
        Next
        Return Content
    End Function

    Public Function GetInstancedObjects(ByVal Content As String) As InstancedObject()
        Dim Data() As InstancedObject
        Dim iRow As Long = -1
        Data = Nothing
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "get_instanced_objects" Then
                Dim r As Regex
                Dim m As Match
                Dim mc As MatchCollection
                r = New Regex(ExpRow("search"), RegexOptions.IgnoreCase)
                mc = r.Matches(Content)
                For Each m In mc
                    iRow += 1
                    ReDim Preserve Data(iRow)
                    Data(iRow).VarName = m.Groups("varname").ToString
                    Data(iRow).ClassName = m.Groups("classname").ToString
                    Data(iRow).ClassCode = ""
                    GetInstancedObjects = Data
                Next
            End If
        Next
        Return Data
    End Function

    Public Function GetClassName(ByVal Content As String) As String
        Dim Data(0) As InstancedObject
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "get_class_name" Then
                Dim r As Regex
                Dim m As Match
                Dim mc As MatchCollection
                r = New Regex(ExpRow("search"), RegexOptions.IgnoreCase)
                mc = r.Matches(Content)
                For Each m In mc
                    Return m.Groups("classname").ToString
                Next
            End If
        Next
        Return ""
    End Function

    Public Function GetClassNameInherits(ByVal Content As String) As String
        Dim Data(0) As InstancedObject
        If Content Is Nothing Then
            Exit Function
        End If
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "get_class_name_inherit" Then
                Dim r As Regex
                Dim m As Match
                Dim mc As MatchCollection
                r = New Regex(ExpRow("search"), RegexOptions.IgnoreCase)
                mc = r.Matches(Content)
                For Each m In mc
                    Return m.Groups("classnameinherit").ToString
                Next
            End If
        Next
        Return ""
    End Function

    Public Function GetVars(ByVal Content As String) As Vars()
        Dim Data() As Vars
        Dim iRow As Long = -1
        Dim AddVar As Boolean = True
        Data = Nothing
        If Content Is Nothing Then
            Exit Function
        End If
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "get_vars" Then
                Dim r As Regex
                Dim m As Match
                Dim mc As MatchCollection
                r = New Regex(ExpRow("search"), RegexOptions.IgnoreCase)
                mc = r.Matches(Content)
                For Each m In mc
                    AddVar = True
                    If iRow > 0 Then
                        For Each Var As Vars In Data
                            If Var.VarName = m.Groups("varname").ToString Then
                                AddVar = False
                            End If
                        Next
                    End If
                    If AddVar Then
                        iRow += 1
                        ReDim Preserve Data(iRow)
                        Data(iRow).VarName = m.Groups("varname").ToString
                        If m.Groups("varambit").ToString = "" Then
                            Data(iRow).VarAmbit = "NONE"
                        Else
                            Data(iRow).VarAmbit = m.Groups("varambit").ToString
                        End If
                        Data(iRow).VarComment = m.Groups("varcomment").ToString
                        GetVars = Data
                    End If
                Next
            End If
        Next
        Return Data
    End Function

    Public Function GetMethods(ByVal Content As String) As Methods()
        Dim Data() As Methods
        Dim iRow As Long = -1
        Dim AddVar As Boolean = True
        Data = Nothing
        If Content Is Nothing Then
            Exit Function
        End If
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "get_methods" Then
                Dim r As Regex
                Dim m As Match
                Dim mc As MatchCollection
                r = New Regex(ExpRow("search"), RegexOptions.IgnoreCase)
                mc = r.Matches(Content)
                For Each m In mc
                    AddVar = True
                    If iRow > 0 Then
                        For Each Var As Methods In Data
                            If Var.MethodName = m.Groups("methodname").ToString Then
                                AddVar = False
                            End If
                        Next
                    End If
                    If AddVar Then
                        iRow += 1
                        ReDim Preserve Data(iRow)
                        Data(iRow).MethodName = m.Groups("methodname").ToString
                        Data(iRow).MethodParameters = m.Groups("parameters").ToString
                        If m.Groups("methodambit").ToString = "" Then
                            Data(iRow).MethodAmbit = "NONE"
                        Else
                            Data(iRow).MethodAmbit = m.Groups("methodambit").ToString
                        End If
                        Data(iRow).MethodComment = ""
                        GetMethods = Data
                    End If
                Next
            End If
        Next
        Return Data
    End Function

    Public Function GetEvents(ByVal Content As String) As Events()
        Dim Data() As Events
        Dim iRow As Long = -1
        Dim AddVar As Boolean = True
        Data = Nothing
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "get_events" Then
                Dim r As Regex
                Dim m As Match
                Dim mc As MatchCollection
                r = New Regex(ExpRow("search"), RegexOptions.IgnoreCase)
                mc = r.Matches(Content)
                For Each m In mc
                    AddVar = True
                    If iRow > 0 Then
                        For Each Var As Events In Data
                            If Var.EventName = m.Groups("eventname").ToString Then
                                AddVar = False
                            End If
                        Next
                    End If
                    If AddVar Then
                        iRow += 1
                        ReDim Preserve Data(iRow)
                        Data(iRow).EventName = m.Groups("eventname").ToString
                        GetEvents = Data
                    End If
                Next
            End If
        Next
        Return Data
    End Function


    Public Function GetProperties(ByVal Content As String) As Vars()
        Dim Data() As Vars
        Dim iRow As Long = -1
        Dim AddVar As Boolean = True
        Data = Nothing
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "get_properties" Then
                Dim r As Regex
                Dim m As Match
                Dim mc As MatchCollection
                r = New Regex(ExpRow("search"), RegexOptions.IgnoreCase)
                mc = r.Matches(Content)
                For Each m In mc
                    AddVar = True
                    If iRow > 0 Then
                        For Each Var As Vars In Data
                            If Var.VarName = m.Groups("varname").ToString Then
                                AddVar = False
                            End If
                        Next
                    End If
                    If AddVar Then
                        iRow += 1
                        ReDim Preserve Data(iRow)
                        Data(iRow).VarName = m.Groups("varname").ToString
                        If m.Groups("varambit").ToString = "" Then
                            Data(iRow).VarAmbit = "NONE"
                        Else
                            Data(iRow).VarAmbit = m.Groups("varambit").ToString
                        End If
                        Data(iRow).VarComment = m.Groups("varcomment").ToString
                        GetProperties = Data
                    End If
                Next
            End If
        Next
        Return Data
    End Function

    Public Function ReplaceBeginClass(ByVal ActualContent As String, ByVal AddContent As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "class_begin" Then
                ActualContent = ExpReplace(ActualContent, ExpRow("search"), "$0" & vbCrLf & " " & AddContent)
            End If
        Next
        Return ActualContent
    End Function

    Public Function ReplaceEndClass(ByVal ActualContent As String, ByVal AddContent As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "class_end" Then
                ActualContent = ExpReplace(ActualContent, ExpRow("search"), AddContent & vbCrLf & "$0" & vbCrLf & " ")
            End If
        Next
        Return ActualContent
    End Function

    Public Function ReplaceClassName(ByVal Content As String, ByVal NewClassName As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "get_class_name" Then
                Content = ExpReplace(Content, ExpRow("search"), "class " & NewClassName)
            End If
        Next
        Return Content
    End Function

    Public Function ReplaceConstructor(ByVal ActualContent As String, ByVal AddContent As String) As String
        For Each ExpRow As DataRow In tb_expressions.Rows
            If ExpRow("type") = "constructor" Then
                'ActualContent = ExpReplace(ActualContent, ExpRow("search"), "$0" & vbCrLf & "   " & AddContent)
                ActualContent = ExpReplace(ActualContent, ExpRow("search"), "$0" & vbCrLf & AddContent)
            End If
        Next
        Return ActualContent
    End Function
End Class
